package com.pointi.emoTools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ezDate
{
	//____________________________________________________________________________________________________
	// DEF

	public static String YYYYMMDD_HHMMSS = "yyyMMdd HH:mm:ss" ;
	public static String YYYYMMDD_HHMMSS_BAR = "yyy-MM-dd HH:mm:ss" ;
	public static String YYYYMMDD = "yyyMMdd" ;
	public static String YYYYMMDD_BAR = "yyy-MM-dd" ;
	public static String YYYYMM = "yyyMM" ;
	public static String YYYYMM_BAR = "yyy-MM" ;
	public static String DAY_OF_WEEK = "u" ;

	//____________________________________________________________________________________________________
	// Date to String

	public static String toString(Date date, String format)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(format) ;
		return sdf.format(date) ;
	}
	
	//____________________________________________________________________________________________________
	// String to Date
	
	public static Date fnStringToDate(String date_str)
	{
		Date date = new Date() ;
		try
		{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd") ;
			date = format.parse(date_str) ;
		} catch (ParseException ex) { ex.printStackTrace() ; }		
		return date ;

	}
	
	//____________________________________________________________________________________________________
	// Set Date-Time to Zero for Date Comapare without Time Info

	public static Date fnSetZeroTime(Date date)
	{
		Calendar cal = Calendar.getInstance() ;
		cal.setTime(date) ;
		cal.set(Calendar.HOUR_OF_DAY, 0) ;
		cal.set(Calendar.MINUTE, 0) ;
		cal.set(Calendar.SECOND, 0) ;
		cal.set(Calendar.MILLISECOND, 0) ;
		return cal.getTime() ;
	}
	
	//____________________________________________________________________________________________________
	// Day of The Week for Business Day
	
	public static int fnGetDayOfWeek(Date date)
	{
		return Integer.valueOf(ezDate.toString(date, DAY_OF_WEEK)) ;
	}
	
	//____________________________________________________________________________________________________
	// Yesterday
	
	public static Date fnGetYesterday(Date date)
	{
		// Based on Business Day
		int a_day = (24 * 60 * 60 * 1000) ;
		int amount = -1 ;
		Date yesterday = new Date(date.getTime() + (amount * a_day)) ;
		if (ezDate.fnGetDayOfWeek(yesterday) == 7) { amount = -3 ; } // MON
		else if (ezDate.fnGetDayOfWeek(yesterday) == 6) { amount = -2 ; } // SUN
		yesterday = new Date(date.getTime() + (amount * a_day)) ;
		return yesterday ;
	}
	
	//____________________________________________________________________________________________________
	// Date Compare without Time

	public static boolean isDateChange(Date date)
	{
		Date new_date = new Date() ;
		String old_date_str = ezDate.toString(date, ezDate.YYYYMMDD_BAR) ;
		String new_date_str = ezDate.toString(new_date, ezDate.YYYYMMDD_BAR) ;
		if (ezDate.fnSetZeroTime(date).before(ezDate.fnSetZeroTime(new_date)))
		{
			System.out.println("[Date Changed] from [" + old_date_str + "] to [" + new_date_str + "]") ;
			return true ;
		}
		return false ; 
	}

	//____________________________________________________________________________________________________
	// EOC
}